import random


HEX_COUNT = 16


def generate_num() -> tuple[list[int], str]:
    nums = []
    input_type = input("请输入要生成的数字类型\n二进制: bin\n十六进制: hex: ")
    size = int(input("请输入需要生成的练习数字数量: "))
    for _ in range(size):
        nums.append(random.randrange(HEX_COUNT))

    for n in nums:
        print(n, end=" ")

    print("\n")

    return nums, input_type


def input_res_num(type="bin") -> list[str]:
    if type == "bin":
        str_arr = input("\n请输入每个数字对应的二进制数:\n").split()
    elif type == "hex":
        str_arr = input("\n请输入每个数字对应的十六进制数:\n").split()
    return str_arr


def judge(nums, arr, input_type) -> float:
    """
    >>> judge([11, 2, 11], [1011, 10, 1011], 'bin')
    1.0
    """

    length = len(arr)
    correct_count = 0

    for i, n in enumerate(arr):
        if input_type == "bin":
            cur_int_n = int(f"0b{n}", 2)
        elif input_type == "hex":
            cur_int_n = int(f"0x{n}", 16)
        else:
            raise

        if nums[i] == cur_int_n:
            correct_count += 1
            continue
        else:
            if input_type == "bin":
                expected_num = bin(nums[i])
            else:
                expected_num = hex(nums[i])
            print(f"转换错误: {nums[i]}\n输入值为: {n}\n正确值为: {expected_num[2:]}")

    return round(correct_count / length, 2)


if __name__ == "__main__":
    # original_arr, input_type = generate_num()
    # str_arr = input_res_num(input_type)
    # correct_ratio = judge(original_arr, str_arr, input_type)
    # print(f"正确率: {int(correct_ratio*100)}%\n")

    import doctest

    doctest.testmod(verbose=True)
